package com.mcxx.ansync.listener;

import com.mcxx.ansync.event.EvtResultRecordEvent;
import com.mcxx.modules.base.entity.EventResultEntity;
import com.mcxx.modules.base.service.write.EventResultWriteService;
import com.mcxx.modules.system.dto.EventResultDTO;
import com.mcxx.util.DateUtil;
import com.yinhai.ta404.core.utils.UUIDUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;


@Component
public class BizEventExecLogListener {
    private final static Logger logger = LoggerFactory.getLogger(BizEventExecLogListener.class);
    @Autowired
    private EventResultWriteService eventResultWriteService;

    @Async
    @Order(10)
    @EventListener(EvtResultRecordEvent.class)
    public void saveEventResultLog(EvtResultRecordEvent evtResultRecordEvent) {
        logger.info("记录异步消息，evtResultRecordEvent: {}, {}", evtResultRecordEvent, DateUtil.now());
        EventResultDTO eventResultDTO = evtResultRecordEvent.getEventResultDTO();
        EventResultEntity eventResultEntity = new EventResultEntity();
        BeanUtils.copyProperties(eventResultDTO, eventResultEntity);
        eventResultEntity.setId(UUIDUtils.getUUID());
        eventResultWriteService.insert(eventResultEntity);
    }

}
